<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Banner extends BaseModel
{
    protected $table = 'banner';

    /**
     * 一个 banner 位会有多个 banner 图
     * @return HasMany
     */
    public function items(): HasMany
    {
        return $this->hasMany(BannerItem::class);
    }

    public static function getBannerById($input): Model|Collection|Builder|array|null
    {
        $bannerField = ['id', 'name'];
        $bannerItemField = ['id', 'banner_id', 'img'];
        return self::with(['items' => function (hasMany $hasMany) use ($bannerItemField) {
            $hasMany->select($bannerItemField)->orderBy('sort');
        }])->find($input['id'], $bannerField);
    }
}
